set(SDL_VERSION "2" CACHE STRING "Version of SDL to use (1.2 or 2)")

if (SDL_VERSION EQUAL "2")
	find_package(SDL2)
	if (NOT SDL2_FOUND)
		include(FindPkgConfig)
		pkg_search_module(SDL2 sdl2)
	endif()

	if (SDL2_FOUND)
		set(SDL_INCLUDE_DIR ${SDL2_INCLUDE_DIRS})
		set(SDL_LIBRARY ${SDL2_LIBRARIES})
		link_directories(${SDL2_LIBDIR})
		set(SDL_VERSION_DEBIAN "2-2.0-0")
		set(SDL_FOUND ON PARENT_SCOPE)

		if (NOT SDL2MAIN_LIBRARY)
			if(TARGET SDL2::SDL2main)
				set(SDL2MAIN_LIBRARY SDL2::SDL2main)
			else()
				set(SDL2MAIN_LIBRARY "")
			endif()
		endif()

		if(APPLE)
			set(CMAKE_OSX_DEPLOYMENT_TARGET "10.7")
		endif()
		set(SDLMAIN_LIBRARY ${SDL2MAIN_LIBRARY})
	endif()
endif()

if(SDL_VERSION EQUAL "1.2" OR NOT SDL2_FOUND)
	find_package(SDL 1.2)
	if(SDL_FOUND)
		set(SDL_VERSION "1.2")
		set(SDL_VERSION_DEBIAN "1.2debian")
		set(USE_PIXMAN ON)
	endif()
endif()

if (NOT SDL2_FOUND AND NOT SDL_FOUND)
	set(SDL_FOUND OFF PARENT_SCOPE)
	set(BUILD_SDL OFF PARENT_SCOPE)
	return()
endif()

set(SDL_VERSION "${SDL_VERSION}" PARENT_SCOPE)
add_definitions(-DBUILD_SDL)

find_feature(USE_PIXMAN "pixman-1")
if(USE_PIXMAN)
	add_definitions(-DUSE_PIXMAN)
	set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libpixman-1.0" PARENT_SCOPE)
endif()

if(WIN32)
	list(APPEND SDL_LIBRARY imm32 setupapi version winmm)
elseif(APPLE)
	list(APPEND SDL_LIBRARY "-framework AppKit" "-framework AudioUnit" "-framework Carbon" "-framework CoreAudio" "-framework AudioToolbox" "-framework ForceFeedback" "-framework IOKit")
	if(NOT CMAKE_SYSTEM_VERSION VERSION_LESS "17.0") # Darwin 17.x is macOS 10.13
		list(APPEND SDL_LIBRARY "-framework Metal")
	endif()
	if(NOT CMAKE_SYSTEM_VERSION VERSION_LESS "19.0") # Darwin 19.x is macOS 10.15
		list(APPEND SDL_LIBRARY "-framework GameController" "-framework CoreHaptics")
	endif()
endif()

if(NOT SDLMAIN_LIBRARY)
	set(SDLMAIN_LIBRARY "")
endif()

set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libsdl${SDL_VERSION_DEBIAN}" PARENT_SCOPE)

file(GLOB PLATFORM_SRC ${CMAKE_CURRENT_SOURCE_DIR}/sdl-*.c)
set(PLATFORM_LIBRARY ${SDL_LIBRARY} ${SDLMAIN_LIBRARY} ${SDL_LIBRARY} ${PIXMAN-1_LIBRARIES})
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${PIXMAN-1_INCLUDE_DIRS} ${SDL_INCLUDE_DIR})

set(SDL_INCLUDE_DIR "${SDL_INCLUDE_DIR}" PARENT_SCOPE)
set(SDL_LIBRARY "${SDL_LIBRARY}" PARENT_SCOPE)
set(SDLMAIN_LIBRARY "${SDLMAIN_LIBRARY}" PARENT_SCOPE)

set(MAIN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/main.c)

if(BUILD_GL)
	list(APPEND MAIN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/gl-sdl.c)
	list(APPEND PLATFORM_SRC ${CMAKE_CURRENT_SOURCE_DIR}/gl-common.c)
endif()
if(BUILD_GLES2)
	list(APPEND MAIN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/gles2-sdl.c)
	list(APPEND PLATFORM_SRC ${CMAKE_CURRENT_SOURCE_DIR}/gl-common.c)
	include_directories(${OPENGLES2_INCLUDE_DIR})
endif()
if(SDL_VERSION EQUAL "2")
	list(APPEND MAIN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/sw-sdl2.c)
else()
	list(APPEND MAIN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/sw-sdl1.c)
endif()

if(ENABLE_SCRIPTING)
	if(BUILD_PYTHON)
		list(APPEND PLATFORM_LIBRARY ${BINARY_NAME}-pylib)
	endif()
endif()

add_executable(${BINARY_NAME}-sdl WIN32 ${PLATFORM_SRC} ${MAIN_SRC})
set_target_properties(${BINARY_NAME}-sdl PROPERTIES COMPILE_DEFINITIONS "${FEATURE_DEFINES};${FUNCTION_DEFINES};${OS_DEFINES}")
target_link_libraries(${BINARY_NAME}-sdl ${BINARY_NAME} ${PLATFORM_LIBRARY} ${OPENGL_LIBRARY} ${OPENGLES2_LIBRARY})
if(NOT WIN32)
	set_target_properties(${BINARY_NAME}-sdl PROPERTIES OUTPUT_NAME ${BINARY_NAME})
else()
	set_target_properties(${BINARY_NAME}-sdl PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
endif()
install(TARGETS ${BINARY_NAME}-sdl DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${BINARY_NAME}-sdl)
if(UNIX)
	install(FILES ${PROJECT_SOURCE_DIR}/doc/mgba.6 DESTINATION ${MANDIR}/man6 COMPONENT ${BINARY_NAME}-sdl)
endif()

debug_strip(${BINARY_NAME}-sdl)
